package me.cloudcat.develop.config;

import me.cloudcat.develop.utils.PropertyUtils;
import org.apache.catalina.filters.RemoteIpFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 注册Filter实例
 *
 * @Author: jiafeim12345@163.com
 * @Date: 2018/3/18/018 19:13
 */
@Configuration
public class WebConfiguration {

    Logger logger = LoggerFactory.getLogger("configuration");

    @Value("123")
    String redisKey;

    @Bean
    public RemoteIpFilter remoteIpFilter() {
        return new RemoteIpFilter();
    }

    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean regBean = new FilterRegistrationBean();
        regBean.setFilter(new MyFilter());
        regBean.addUrlPatterns("/*");
        regBean.addInitParameter("name", " 123");
        regBean.setName("MyFilter");
        regBean.setOrder(1);
        return regBean;
    }

    class MyFilter implements Filter {
        @Override
        public void destroy() {
            // TODO Auto-generated method stub
        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
                throws IOException, ServletException {
            // TODO Auto-generated method stub
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            System.out.println("this is MyFilter, url :" + request.getRequestURI());
//            System.out.println(PropertyUtils.getString("mq.vhost"));
            filterChain.doFilter(servletRequest, servletResponse);
        }

        @Override
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
        }
    }
}
